package wjt.controller;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import wjt.vo.Product;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/consumer")
public class ConsumerProductController {
    /**
     *
     * 引入ribbon后的链接调整
     */
    public static final String PRODUCT_GET_URL = "http://SPRINGCLOUD-PROVIDER-EUREKA/product/get/";
    public static final String PRODUCT_LIST_URL = "http://SPRINGCLOUD-PROVIDER-EUREKA/product/list/";

    @Resource
    private RestTemplate restTemplate;

    /**
     * 没有引入springcloud-security安全模块前的配置
     */
    /*@RequestMapping("/product/get")
    public Object getProduct(long id) {
        Product product = restTemplate.getForObject(PRODUCT_GET_URL + id, Product.class);
        return  product;
    }

    @RequestMapping("/product/list")
    public  Object listProduct() {
        List<Product> list = restTemplate.getForObject(PRODUCT_LIST_URL, List.class);
        return  list;
    }

    @RequestMapping("/product/add")
    public Object addPorduct(Product product) {
        Boolean result = restTemplate.postForObject(PRODUCT_ADD_URL, product, Boolean.class);
        return  result;
    }*/


    /**
     * 引入springcloud-security安全模块后的配置
     */
    @Resource
    private HttpHeaders httpHeaders;

    @RequestMapping("/product/get")
    public Object getProduct(long id) {
        Product product = restTemplate.exchange(PRODUCT_GET_URL + id,HttpMethod.GET,new HttpEntity<>(httpHeaders), Product.class).getBody();
        return  product;
    }

    @RequestMapping("/product/list")
    public  Object listProduct() {
        List<Product> list = restTemplate.exchange(PRODUCT_LIST_URL, HttpMethod.GET,new HttpEntity<>(httpHeaders), List.class).getBody();
        return  list;
    }

}